package org.example.yunshu.demos.service;

import org.example.yunshu.demos.constant.Constants;
import org.example.yunshu.demos.mapper.FriendMapper;
import org.example.yunshu.demos.mapper.MessageMapper;
import org.example.yunshu.demos.mapper.SessionMapper;
import org.example.yunshu.demos.model.Add;
import org.example.yunshu.demos.model.Friend;
import org.example.yunshu.demos.model.Result;
import org.example.yunshu.demos.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class FriendService {
    @Autowired
    private FriendMapper friendMapper ;

    @Autowired
    private SessionMapper sessionMapper ;

    @Autowired
    private MessageMapper messageMapper ;

    public Object getFriendListByUserId(HttpSession httpSession) {
       UserInfo userInfo = (UserInfo) httpSession.getAttribute(Constants.USERINFO_KEY);
       List<Friend> list = friendMapper.getFriendListByUserId(userInfo);
       return list ;
    }

    //
    public Object deleteFriend(Integer userId, Integer sessionId, String friendName) {

       Integer friendId = friendMapper.selectFriendId(friendName) ;

      int res =   sessionMapper.removeSession(sessionId);
      sessionMapper.removeSessionUser(sessionId);

       messageMapper.removeMessage(sessionId);
       res += friendMapper.deleteFriend(userId , friendId) ;

        return Result.success(res >= 1);
    }


    public Object searchFriends(String keyword, Integer userId) {

       return friendMapper.search(keyword , userId);
    }

    public Object getAddList(Integer userId) {
        return friendMapper.getAddList(userId);
    }
}
